package com.zhiyou100.aspect;

import java.lang.reflect.Method;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.annotation.After;
import org.aspectj.lang.annotation.AfterReturning;
import org.aspectj.lang.annotation.Aspect;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.ui.Model;
import org.springframework.web.context.request.RequestAttributes;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

import com.zhiyou100.mapper.AdminMapper;
import com.zhiyou100.util.MyLogAnno;



// 声明是一个事务切面
@Component
@Aspect
public class MyLogAspect {
	
	// 偷个懒,不经过 Service 层啦
	@Autowired
	AdminMapper mapper;
	
	
	// 使用后置返回通知
	/*
	 * Before 前置通知
	 * AfterReturning 后置通知
	 * Around 环绕通知
	 * AfterThrowing 抛出异常通知
	 * After 最终通知
	 */
	
	//@AfterReturning("@annotation(com.zhiyou100.util.MyLogAnno)")
	@After("@annotation(com.zhiyou100.util.MyLogAnno)")
	public void after(JoinPoint joinPoint) {  // After 只能有这一个注解,否则就会报错
		System.out.println("后置通知");
		// 获得注解中的值
		String desc = getAnnoDesc(joinPoint);
		System.out.println("请求的方法 : "+desc);
		// 以下代码需要使用Springmvc,既控制层也需要由Spring托管,才会获得ip
		// HttpServletRequest request = ((ServletRequestAttributes) RequestContextHolder.getRequestAttributes()).getRequest();
		RequestAttributes requestAttributes = RequestContextHolder.getRequestAttributes();
		ServletRequestAttributes servletRequestAttributes = (ServletRequestAttributes) requestAttributes;
		HttpServletRequest request = servletRequestAttributes.getRequest();
		// ip
		String ip = request.getRemoteAddr();
		System.out.println("请求对象的ip : "+ip);
		// 获得用户信息
		System.out.println("对象是 : "+request.getSession().getAttribute("mName"));
//		MUser mUser = (MUser) request.getSession().getAttribute("mName");
//		System.out.println("请求对象的信息是 : "+mUser);
		// 获得时间
		Date date = new Date();
//		System.out.println("时间 : "+new Date());
		System.out.println("时间 : "+date);
		
		Map map = new HashMap();
		map.put("mydesc", desc);
		map.put("ip", ip);
		map.put("mUser",request.getSession().getAttribute("mName"));
		map.put("date", date);
		if(map.isEmpty()) {
			
		}else {
//			int row =mapper.MyLog(map);
		}
		
	}

	
	// 封装的方法,获得目标方法上的注解的值
	// 如果这个方法是私有的,则不会调用这个方法,导致空指针
	public static String getAnnoDesc(JoinPoint joinPoint) {
		
		String desc = "";
		// 获得目标方法名
		Object methodName = joinPoint.getSignature().getName();
		// 获得目标方法的参数
		Object[] args = joinPoint.getArgs();
		// 获得目标方法的全部方法对象
		Method[] methods = joinPoint.getTarget().getClass().getDeclaredMethods();
		// 如果该类中的方法与目标方法名一致
		for(Method method : methods) {
			Class<?>[] types = method.getParameterTypes();
			if(method.getName().equals(methodName)) {
				// 判断参数个数是否一致,防止是重载的方法
				if(args.length == types.length) {
					desc = method.getAnnotation(MyLogAnno.class).value();
				}
				
			}
		}
		return desc;
	}
	
	
	
}
